package handler;

import java.io.IOException;
import java.util.List;
import java.util.Vector;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import pojo.Merchant;

@Controller(value="merchantWebSocketHandler")
@RequestMapping("/ws/merchant")
public class MerchantWebSocketHandler extends TextWebSocketHandler{
    private List<WebSocketSession> sessionList =new Vector<WebSocketSession>();

    public List<WebSocketSession> getSessionList() {
        return sessionList;
    }

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) {
        //接收到客户端消息时调用
        System.out.println("text message: " + session.getId() + "-" + message.getPayload());
        System.out.println("user"+session.getAttributes().get("user"));
        for(WebSocketSession sen: sessionList){
            try {
                sen.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session)
            throws Exception {
        // 与客户端完成连接后调用
        System.out.println("afterConnectionEstablished");
        System.out.println("getId:" + session.getId());
        System.out.println("getLocalAddress:" + session.getLocalAddress().toString());
        System.out.println("getTextMessageSizeLimit:" + session.getTextMessageSizeLimit());
        System.out.println("getUri:" + session.getUri().toString());
        System.out.println("getPrincipal:" + session.getPrincipal());
//        session.sendMessage(new TextMessage("你好".getBytes()));
        sessionList.add(session);
    }

    @Override
    public void handleTransportError(WebSocketSession session,
                                     Throwable exception) throws Exception {
        // 消息传输出错时调用
        System.out.println("handleTransportError");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session,
                                      CloseStatus closeStatus) throws Exception {
        sessionList.remove(session);
        // 一个客户端连接断开时关闭
        System.out.println("afterConnectionClosed");
    }
    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
    
    public void sendTo(String merchantId,String msg) {
    	for(WebSocketSession sen: sessionList){
            try {
            	Merchant merchant = (Merchant) sen.getAttributes().get("user");
            	System.out.println(merchantId+":"+merchant.getId().equals(merchantId));
               if (merchant!=null&&merchant.getId().equals(merchantId)) {
            	   System.out.println(msg);
				sen.sendMessage(new TextMessage(msg));
			}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public void sendToByName(String merchantName,String msg) {
    	for(WebSocketSession sen: sessionList){
            try {
            	Merchant merchant = (Merchant) sen.getAttributes().get("user");
            	System.out.println(merchant);
               if (merchant!=null&&merchant.getName().equals(merchantName)) {
				sen.sendMessage(new TextMessage(msg));
			}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}